package cn.tedu.charging.order.timer;

import cn.tedu.charging.common.pojo.po.ChargingBillExceptionPO;
import cn.tedu.charging.common.pojo.po.ChargingBillSuccessPO;
import cn.tedu.charging.common.protocol.WebSocketMessage;
import cn.tedu.charging.order.dao.mapper.ChargingBillExceptionMapper;
import cn.tedu.charging.order.dao.mapper.ChargingBillSuccessMapper;
import cn.tedu.charging.order.websocket.WebSocketServer;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xxl.job.core.context.XxlJobContext;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Slf4j
@Component
public class OrderExpCheckTimer {
    @Autowired
    private ChargingBillSuccessMapper chargingBillSuccessMapper;
    //exceptionMapper
    @Autowired
    private ChargingBillExceptionMapper chargingBillExceptionMapper;
    @Autowired
    private WebSocketServer webSocketServer;

    @XxlJob("checkSuccessStatus")
    public void checkSuccessStatus(String timerParam){
        log.debug("定时任务");
        timerParam= XxlJobContext.getXxlJobContext().getJobParam();
       String orderNo= parseOrderNo(timerParam);
       Integer userId= parseUserId(timerParam);
        ChargingBillSuccessPO po = getSuccessByBillId(orderNo);
        if (po==null){
            log.debug("订单不存在");
            return;
        }
        Integer billStatus = po.getBillStatus();
        log.debug("订单id为{}",billStatus);
        if(billStatus!=null && billStatus==1){
            updateSuccessStatus(orderNo,3);
            //生成一个异常单
            saveSuccessException(orderNo);
            WebSocketMessage<String> webSocketMessage = new WebSocketMessage<>();
            webSocketMessage.setState(1);
            webSocketMessage.setMessage("exception");
            webSocketMessage.setData("A 您的订单存在异常,送您一张优惠券");
            webSocketServer.pushMessage(userId, JSON.toJSONString(webSocketMessage));
        }else {
            return;
        }
    }

    private ChargingBillSuccessPO getSuccessByBillId(String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bill_id",orderNo);
        return chargingBillSuccessMapper.selectOne(queryWrapper);
    }

    private void saveSuccessException(String orderNo) {
        ChargingBillExceptionPO exceptionPO=new ChargingBillExceptionPO();
        exceptionPO.setBillId(orderNo);
        //剩余的要从任务发起者 填补任务参数
        exceptionPO.setElectricity(new BigDecimal("1.09"));
        chargingBillExceptionMapper.insert(exceptionPO);
    }

    private void updateSuccessStatus(String orderNo, int status) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.setSql("bill_status="+status);
        updateWrapper.eq("order_no",orderNo);
        chargingBillSuccessMapper.update(null,updateWrapper);
    }

    private Integer parseUserId(String timerParam) {
        JSONObject jsonObject = JSONObject.parseObject(timerParam);
        return Integer.parseInt((String) jsonObject.get("userId")) ;
    }

    private String parseOrderNo(String timerParam) {
        JSONObject jsonObject = JSONObject.parseObject(timerParam);
        return (String) jsonObject.get("orderNo");
    }
}
